package gnss

import (
	"dgo/goutils/utils"
	"gitee.com/ymofen/gobase"
)

var (
	unicore_range_prebuf   = []byte{0xAA, 0x44}
	unicoreb5_sync_buf     = []byte{0xAA, 0x44, 0xB5}
	unicore_range_sync_buf = []byte{0xAA, 0x44, 0x12}
)

const (
	unicore_max_len = 10240
)

type UnicoreDecoder struct {
	decodeStep      int8
	msgid           uint16
	bodystart       uint16
	bodyl           uint16
	cache           utils.BytesBuilder
	innerDecodeFunc func() int
	/**
	  packBuf 整个包的Buf
	*/
	OnPackage func(msgid uint16, packbuf []byte)

	/**
	  packBuf 整个包的Buf,
	  返回-1:会重新解码
	*/
	OnPackageEx func(msgid uint16, packbuf []byte) int
}

func NewUnicoreDecoder() *UnicoreDecoder {
	rval := &UnicoreDecoder{}
	return rval
}

/*
*
包括头+DATA+CRC
*/
func (this *UnicoreDecoder) PackageBuf() []byte {
	return this.cache.Bytes()
}

func (this *UnicoreDecoder) BodyBuf() []byte {
	return this.cache.Buff(int(this.bodystart), int(this.bodyl))
}

func (this *UnicoreDecoder) innerDecodeRange() int {
	l := this.cache.Len()
	if l == 4 {
		if this.cache.Byte(l-1) != 0x1C { // 长度28(0x1C)
			this.decodeStep = -1
			return -1
		}
	} else if l == 6 {
		this.msgid = this.cache.Uint16_LE(4)
	} else if l == 10 {
		this.bodystart = 28
		this.bodyl = this.cache.Uint16_LE(8)
		if this.bodyl > unicore_max_len {
			this.decodeStep = -1
			return -1
		}
	} else {
		if l == 28+int(this.bodyl)+4 {
			return this.doPackageDone()
		}
	}
	return 0

}

func (this *UnicoreDecoder) doPackageDone() int {
	evt := this.OnPackageEx
	if evt != nil {
		r := evt(this.msgid, this.cache.Bytes())
		if r == -1 {
			this.decodeStep = -1
			return -1
		}
		this.decodeStep = 0
		return 0
	}

	{
		evt1 := this.OnPackage
		if evt1 != nil {
			evt1(this.msgid, this.cache.Bytes())
		}

		this.decodeStep = 0
		return 1
	}

}

func (this *UnicoreDecoder) innerDecodeB5() int {
	l := this.cache.Len()
	if l == 6 {
		this.msgid = this.cache.Uint16_LE(4)
	} else if l == 8 {
		this.bodyl = this.cache.Uint16_LE(6)
		if this.bodyl > unicore_max_len {
			this.decodeStep = -1
			return -1
		}
	} else {
		this.bodystart = 24
		if l == 24+int(this.bodyl)+4 {
			//if this.OnPackage != nil {
			//	this.OnPackage(this.msgid, this.cache.Bytes())
			//}
			//this.decodeStep = 0
			return this.doPackageDone()
			//return 1
		}
	}
	return 0
}

func (this *UnicoreDecoder) InputByte(v byte) int {
	if this.decodeStep <= 0 {
		if this.decodeStep == -1 {
			this.cache.Cleanup()
		} else {
			this.cache.Reset()
		}

		this.bodystart = 0
		this.bodyl = 0
		this.decodeStep = 1
	}

	this.cache.WriteByte(v)
	if this.decodeStep == 1 {
		l := this.cache.Len()
		if l == 3 {
			if this.cache.Index(0, unicoreb5_sync_buf) == 0 {
				this.decodeStep = 2
				this.innerDecodeFunc = this.innerDecodeB5
				return 0
			}

			if this.cache.Index(0, unicore_range_sync_buf) == 0 {
				this.decodeStep = 2
				this.innerDecodeFunc = this.innerDecodeRange
				return 0
			}

			// 需要重新解码
			this.decodeStep = -1
			newBuf := gobase.CloneBytes(this.cache.Buff(1, 0), 0, 0)
			for j := 0; j < len(newBuf); j++ {
				this.InputByte(newBuf[j])
			}
			return 0
		}
	} else if this.decodeStep == 2 {
		r := this.innerDecodeFunc()
		if r == -1 {
			this.decodeStep = -1
			newBuf := gobase.CloneBytes(this.cache.Buff(1, 0), 0, 0)
			for j := 0; j < len(newBuf); j++ {
				this.InputByte(newBuf[j])
			}
		}
		return 0
	}
	return 0
}

func (this *UnicoreDecoder) InputBuf(buf []byte) {
	for i := 0; i < len(buf); i++ {
		this.InputByte(buf[i])
	}
}
